/* -----------------
* brief
* 1. this is code gen by tools
* 2. for add/update/delete/query the data save in sql database
 */

package user

import (
	"github.com/go-xorm/xorm"
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	"saas_sys_base/common"
	"saas_sys_base/redishelper"

	//"github.com/mjiulee/lego/utils"
)

const REDIS_USER_BY_ID_PREFIX string = "user_by_id:"
const REDIS_USER_BY_WX_UID_PREFIX string = "user_by_wx_uid:"
const REDIS_USER_BY_WEB_UID_PREFIX string = "user_by_web_uid:"


/*
实验性代码
 */
func (t *User) CacheUpdateAllBy(item *User) {
	_ = redishelper.RedisSetKeyEx(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(item.Id), item)
	_ = redishelper.RedisSetKeyEx(REDIS_USER_BY_WX_UID_PREFIX+utils.Int64ToString(item.WxUid), item)
	_ = redishelper.RedisSetKeyEx(REDIS_USER_BY_WEB_UID_PREFIX+utils.Int64ToString(item.WebUid), item)
}

func (t *User) CacheDelAllBy(item *User) {
	_ = redishelper.RedisDelKey(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(item.Id))
	_ = redishelper.RedisDelKey(REDIS_USER_BY_WX_UID_PREFIX+utils.Int64ToString(item.WxUid))
	_ = redishelper.RedisDelKey(REDIS_USER_BY_WEB_UID_PREFIX+utils.Int64ToString(item.WebUid))
}

/* 根据id获取记录
 * -----------------
 */
func (t *User) RecodeById(id int64) *User {
	item := new(User)

	err := redishelper.RedisGetKeyEx(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(id), item)
	if err == nil {
		return item
	}

	_, err = lego.GetDBEngine().ID(id).Where("if_del = ?", 0).Get(item)
	if item.Id <= 0 || err != nil {
		return nil
	}

	t.CacheUpdateAllBy(item)

	return item
}

func (t *User) RecodeByWxUid(WxUid int64) *User {
	item := new(User)
	err := redishelper.RedisGetKeyEx(REDIS_USER_BY_WX_UID_PREFIX+utils.Int64ToString(WxUid), item)
	if err == nil {
		return item
	}
	lego.GetDBEngine().Where("if_del = ?", 0).And("wx_uid=?", WxUid).Get(item)
	if item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)

	return item
}

func (t *User) RecodeByWebUid(WebUid int64) *User {
	item := new(User)
	err := redishelper.RedisGetKeyEx(REDIS_USER_BY_WEB_UID_PREFIX+utils.Int64ToString(WebUid), item)
	if err == nil {
		return item
	}

	lego.GetDBEngine().Where("if_del = ?", 0).And("web_uid=?", WebUid).Get(item)
	if item.Id <= 0 {
		return nil
	}

	t.CacheUpdateAllBy(item)
	return item
}

/* 添加
 * -----------------
 */
func (t *User) AddRecode(item2add *User) bool {
	item2add.Id = lego.UUID()
	_, err := lego.GetDBEngine().Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	// TODO 这里应该不需要操作缓存
	//_ = redishelper.RedisDelKey(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	//_ = redishelper.RedisDelKey(REDIS_USER_BY_WX_ID_PREFIX+utils.Int64ToString(item2add.WxUid))

	return true
}

func (t *User) AddRecodeWithsession(session *xorm.Session, item2add *User) bool {
	item2add.Id = lego.UUID()
	_, err := session.Insert(item2add)
	if err != nil {
		lego.LogError(err)
		return false
	}

	//_ = redishelper.RedisDelKey(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(item2add.Id))
	//_ = redishelper.RedisDelKey(REDIS_USER_BY_WX_ID_PREFIX+utils.Int64ToString(item2add.WxUid))
	return true
}

/* 删除(硬删除)*/
func (t *User) DelRecodeById(id int64) bool {
	item := new(User)

	_, err := lego.GetDBEngine().ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	itemDel := new(User)
	_ = redishelper.RedisGetKeyEx(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(itemDel)


	return true
}

func (t *User) DelRecodeByIdWithSession(session *xorm.Session, id int64) bool {
	item := new(User)

	_, err := session.ID(id).Delete(item)
	if err != nil {
		lego.LogError(err)
		return false
	}

	itemDel := new(User)
	_ = redishelper.RedisGetKeyEx(REDIS_USER_BY_ID_PREFIX+utils.Int64ToString(id), itemDel)
	t.CacheDelAllBy(item)
	return true
}

/* 更新
 * -----------------*/
func (t *User) UpdateRecode(rc *User) bool {
	_, err := lego.GetDBEngine().ID(rc.Id).Omit("id", "if_del").Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	t.CacheDelAllBy(rc)
	return true
}

func (t *User) UpdateRecodeWithSession(session *xorm.Session, rc *User) bool {
	_, err := session.ID(rc.Id).Update(rc)
	if err != nil {
		lego.LogError(err)
		return false
	}

	t.CacheDelAllBy(rc)

	return true
}

type Result struct {

}

func (t *User) ListUserByApp2(who common.Who, keyword string, page, psize int) ([]Result, int64, error) {
	start := (page-1*psize)
	resultList := make([]Result, 0)

	session := lego.GetDBEngine().Table("user").Alias("t1").
		Join("INNER", []string{"group", "t2"}, "group.id = t2.group_id").
		//Join("INNER", "tb_xx", "tb_xx.id = tb_xx.type_id").
		Where("if_del = ?", 0).
		And("fxxx = ?", 0).
		Desc("t1.field_xxx").
		Asc()
	//OrderBy("asc")

	session2 := *session.Clone()
	err := session.Limit(psize, start).Find(&resultList)
	if err != nil {
		return nil, 0, err
	}

	total, err := session2.Count()
	if err != nil {
		return nil, 0, err
	}
	return resultList, total, err
}
